package com.tianji.learning.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.user.UserDTO;
import com.tianji.learning.domain.po.PointsBoard;
import com.tianji.learning.domain.query.PointsBoardQuery;
import com.tianji.learning.domain.vo.PointsBoardItemVO;
import com.tianji.learning.domain.vo.PointsBoardVO;
import com.tianji.learning.mapper.PointsBoardMapper;
import com.tianji.learning.service.IPointsBoardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.tianji.common.utils.DateUtils.DATE_SUFFIX_FORMATTER;
import static com.tianji.learning.constants.LearningConstants.POINTS_BOARD_TABLE_PREFIX;
import static com.tianji.learning.constants.RedisConstants.POINTS_BOARD_KEY_PREFIX;

/**
 * <p>
 * 学霸天梯榜 服务实现类
 * </p>
 *
 * @author 老李头儿
 * @since 2025-03-31
 */
@Service
@RequiredArgsConstructor
public class PointsBoardServiceImpl extends ServiceImpl<PointsBoardMapper, PointsBoard> implements IPointsBoardService {
    private final UserClient userClient;


    /**
    * @Description 分页查询赛季积分 TODO
    * @Author 老李头儿
    * @Date 2025年04月02 10:37:37
    * @param query
    * @Return java.lang.String
    **/
    @Override
    public PointsBoardVO queryPage(PointsBoardQuery query) {
        //判断是否在当前赛季
        Long season = query.getSeason();
        boolean isCurrentSeason = ObjectUtils.isEmpty(season) || season == 0;

        //查询指定赛季我的积分信息
        String key = POINTS_BOARD_KEY_PREFIX + LocalDateTime.now().format(DATE_SUFFIX_FORMATTER);
        PointsBoard myBoard = isCurrentSeason ? queryMyCurrentBoard(key) : queryMyHistoryBoard(season);

        //查询指定赛季前100名积分信息
        List<PointsBoard> boards = isCurrentSeason ?
                queryCurrentBoards(key,query.getPageNo(),query.getPageSize()) : queryHistoryBoards(query);
        //封装返回结果
        PointsBoardVO vo = new PointsBoardVO();
        vo.setPoints(myBoard.getPoints());
        vo.setRank(myBoard.getRank());
        //封装积分排行榜信息
        if(ObjectUtils.isEmpty(boards)){
            return vo;
        }
        //查询前100名积分信息(用户信息)
        Set<Long> userIds = boards.stream().map(PointsBoard::getUserId).collect(Collectors.toSet());
        List<UserDTO> userInfoList = userClient.queryUserByIds(userIds);
        Map<Long, String> userInfoMap = new HashMap<>(userIds.size());
        if(ObjectUtils.isNotEmpty(userInfoList)){
            userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserDTO::getId, UserDTO::getName));
        }
       //循环组装itemlist
        List<PointsBoardItemVO> boardList = new ArrayList<>(boards.size());
        for (PointsBoard board : boards) {
            PointsBoardItemVO itemVO = new PointsBoardItemVO();
            itemVO.setPoints(board.getPoints());
            itemVO.setRank(board.getRank());
            itemVO.setName(userInfoMap.getOrDefault(board.getUserId(),""));
            boardList.add(itemVO);
        }

        vo.setBoardList(boardList);
        return vo;

    }

    /**
    * @Description 查询历史赛季前100名积分信息
    * @Author 老李头儿
    * @Date 2025年04月03 14:26:35
    * @param query
    * @Return java.util.List<com.tianji.learning.domain.po.PointsBoard>
    **/

    private List<PointsBoard> queryHistoryBoards(PointsBoardQuery query) {
        //
    }

    /**
    * @Description 查询当前赛季前100名积分信息
    * @Author 老李头儿
    * @Date 2025年04月03 14:25:56
    * @param key
    * @param pageNo
    * @param pageSize
    * @Return java.util.List<com.tianji.learning.domain.po.PointsBoard>
    **/

    private List<PointsBoard> queryCurrentBoards(String key, @Min(value = 1, message = "页码不能小于1") Integer pageNo, @Min(value = 1, message = "每页查询数量不能小于1") Integer pageSize) {
        //分页从redis中查询数据
    }

    /**
    * @Description 查询历史赛季我的积分信息
    * @Author 老李头儿
    * @Date 2025年04月03 14:25:38
    * @param season
    * @Return com.tianji.learning.domain.po.PointsBoard
    **/

    private PointsBoard queryMyHistoryBoard(Long season) {
    }

    /**
    * @Description 查询当前赛季我的积分信息
    * @Author 老李头儿
    * @Date 2025年04月03 14:25:16
    * @param key
    * @Return com.tianji.learning.domain.po.PointsBoard
    **/

    private PointsBoard queryMyCurrentBoard(String key) {
    }

    /**
    * @Description 定时创建表
    * @Author 老李头儿
    * @Date 2025年04月03 10:45:58
    * @param season
    * @Return void
    **/

    @Override
    public void createPointsBoardTableBySeason(Integer season) {
        this.getBaseMapper().createTable(POINTS_BOARD_TABLE_PREFIX + season);

    }
}
